package com.fairytask.engine.components.clip
{
	import com.adobe.utils.StringUtil;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.ResManager;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.core.DisplayObjectLoader;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.LoaderInfo;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
//例子****************************************************************************
//	var val:NxSwfBackground	=	new NxSwfBackground(null,false,function():void
//	{
//		//加载成功后
//		val.mouseEnabled	=	false;
//		val.mouseChildren	=	false;
//	});
//	//设置加载资源，可以使地址，也可以使显示对象
//	val.source	=	"aa.swf";	
//	val.alpha	=	.5
//	val.x		=	150
//	val.y		=	50
//	addChild(val);
//****************************************************************************

	/**
	 * 外部资源容器
	 * 功能：方便外部资源管理，用于临时加载的资源显示对象
	 * @author Hanjy
	 * 
	 */
	public class Clip extends BaseComponent
	{
		private	var _isCache		:Boolean;
		
		private	var _source			:DisplayObject;
		
		private 	var _onLoadComplete	:Function;
		
		private 	var _isClear		:Boolean	=	false;
		
		private	var _parentClass	:Object;
		
		private	var _defaultImg		:DisplayObject;
		
		private	var _width			:Number		=	-1;
		
		private	var _height			:Number		=	-1;
		
		private 	var _data			:Object;
		
		private var _needResize		:Boolean	=	false;
		
		private var _loadCount			:int	=	0;//加载计数
		//加载图片
		private var _loadingRes		:String	=	"";
		private var _resPath			:String =  	"";
		private var loadOper		:DisplayObjectLoader;
		
		/**
		 * 自定义显示对象
		 * @param PointID	：对象ID
		 * @param isCache	：是否缓存
		 * 
		 */
		public function Clip(isCache:Boolean = false,onLoadComplete:Function = null,defaultImage:String = null)
		{
			_onLoadComplete	=	onLoadComplete;
			_isCache		=	isCache;
			if(defaultImage!=null)
			{
				//source=defaultImage;
				loadingRes	=	defaultImage;
			}
		}
		
		public function get parentClass():Object
		{
			return _parentClass;
		}
		
		public function set parentClass(val:Object):void
		{
			_parentClass	=	val;
		}
		
		public function removeEvent():void
		{
			_onLoadComplete	=	null;
		}
		
		public function addLoadedEvent(func:Function):void
		{
			_onLoadComplete	=	func;
		}
		
		public function get source():Object
		{
			return _source;
		}
		
		public function set source(val:Object):void
		{
			//IO.traceLog("添加显示对象了..........");
			_isClear	=	false;
			
			var tRes:DisplayObject;
			
			if(val is String)
			{
				if(val == "")
				{
					return;	
				}
				
				_resPath = val as String;
				
				if(_defaultImg!=null)
				{
					addRes(_defaultImg);
				}
				
				_needResize	=	false;
				
				tRes	=	ResManager.newResClass(String(val));
				if(tRes == null)
				{
					_resPath = _resPath.replace(ResConfig.uiBGPath,"");
					tRes	=	ResManager.newResClass(String(_resPath));
				}
				
				var canCache	:Boolean	=	true;
				if(StringUtil.contains(String(val),".swf")>0)
				{
					canCache	=	false;
				}
				
				if(tRes == null)
				{
					if(canCache)
					{
						loadOper	=	new ImageLoader(val,{onComplete:completeHandler});
					}
					else
					{
						loadOper	=	new SWFLoader(val,{onComplete:completeHandler});
					}
					loadOper.load();
				}
				
				if(tRes != null)
				{
					addRes(tRes);
				}
			}
			else if(val is DisplayObject)
			{
				addRes(val as DisplayObject)
			}
		}
		
		/**
		 * 图形加载完毕 
		 * @param event
		 * 
		 */
		private function completeHandler(event:LoaderEvent):void
		{
			if(null!=loadOper.rawContent)
			{
				addRes(loadOper.rawContent);
			}
		}
		
		/**
		 * 添加对象 
		 * @param val
		 * 
		 */
		private function addRes(val:DisplayObject):void
		{
			//如果已经清除，就不再添加（加载延迟用）
			if(_isClear)
			{
				return;
			}
			clear();
			if(val == null)
			{
				//加载失败了，但是地址是有的，就重新加载吧 
				if(_resPath!=""&&_loadCount<3)
				{
					source	=	_resPath;
				}
				else
				{
					//告诉外面加载失败了
					dispatchEvent(new ClipEvent(ClipEvent.RES_LOADERROR));
				}
				_loadCount++;
				return;
			}
			
			if(val == _defaultImg)
			{
				addChild(val);
			}
			else
			{
				if(val is Bitmap)
				{
					if(null != _source)
					{
						_needResize = true;
					}
					_source = new Bitmap((val as Bitmap).bitmapData.clone());
				}
				else
				{
					_source = val;
				}
				if(!_needResize)
				{
					_width	=	_source.width;
					_height	=	_source.height;
				}
				else
				{
					_source.width	=	_width;
					_source.height	=	_height;
				}
				
				addChild(_source);
				
				if(_onLoadComplete != null)
				{
					_onLoadComplete.call(this,this);
				}
			}
			
			if(_width != -1)
			{
				_source.width	=	_width;
			}
			
			if(_height != -1)
			{
				_source.height	=	_height;
			}
		}
		
		/**
		 * 检查加载完对象是否是最新的 ，最新true,旧的false
		 * @param val
		 * 
		 */
		private function checkRes(val:String):Boolean
		{
			if(val.indexOf(_resPath,0) == -1)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		
		private function onLoading(evt:ProgressEvent):void
		{
			if(source)
			{
				source.x	=	source.y	=	0;
				source.width	=	_width;
				source.height	=	_height;
				addChild(source as DisplayObject);
			}
		}
		
		public	function clear():void
		{
			while(this.numChildren>0)
			{
				this.removeChildAt(0);
			}
			_isClear	=	true;
		}

		override public function get height():Number
		{
			return _height;
		}

		override public function set height(value:Number):void
		{
			_height = value;
			if(_source != null)
			{
				_source.height	=	_height;
			}
			else
			{
				_needResize	=	true;
			}
		}

		override public function get width():Number
		{
			return _width;
		}

		override public function set width(value:Number):void
		{
			_width = value;
			if(_source != null)
			{
				_source.width	=	_width;
			}
			else
			{
				_needResize	=	true;
			}
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}

		public function get loadingRes():String
		{
			return _loadingRes;
		}

		public function set loadingRes(value:String):void
		{
			_loadingRes =	value;
			_defaultImg	=	ResManager.newResClass(value);
		}


	}
}